package com.example.orderflow.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.example.orderflow.entity.enums.PageSize;
import com.example.orderflow.entity.query.ApproveHistoryQuery;
import com.example.orderflow.entity.po.ApproveHistory;
import com.example.orderflow.entity.vo.PaginationResultVO;
import com.example.orderflow.entity.query.SimplePage;
import com.example.orderflow.mappers.ApproveHistoryMapper;
import com.example.orderflow.service.ApproveHistoryService;
import com.example.orderflow.utils.StringTools;


/**
 *  业务接口实现
 */
@Service("approveHistoryService")
public class ApproveHistoryServiceImpl implements ApproveHistoryService {

	@Resource
	private ApproveHistoryMapper<ApproveHistory, ApproveHistoryQuery> approveHistoryMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<ApproveHistory> findListByParam(ApproveHistoryQuery param) {
		return this.approveHistoryMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(ApproveHistoryQuery param) {
		return this.approveHistoryMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<ApproveHistory> findListByPage(ApproveHistoryQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<ApproveHistory> list = this.findListByParam(param);
		PaginationResultVO<ApproveHistory> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(ApproveHistory bean) {
		return this.approveHistoryMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<ApproveHistory> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.approveHistoryMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<ApproveHistory> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.approveHistoryMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(ApproveHistory bean, ApproveHistoryQuery param) {
		StringTools.checkParam(param);
		return this.approveHistoryMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(ApproveHistoryQuery param) {
		StringTools.checkParam(param);
		return this.approveHistoryMapper.deleteByParam(param);
	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public ApproveHistory getApproveHistoryById(Integer id) {
		return this.approveHistoryMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateApproveHistoryById(ApproveHistory bean, Integer id) {
		return this.approveHistoryMapper.updateById(bean, id);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteApproveHistoryById(Integer id) {
		return this.approveHistoryMapper.deleteById(id);
	}

	/**
	 * 根据订单编号、当前等级和执行人名称获取对象
	 */
	@Override
	public ApproveHistory getApproveHistoryByOrderIdAndLevelIdAndApproveBy(String orderId, String levelId, String approveBy){
		return this.approveHistoryMapper.selectByOrderIdAndLevelIdAndApproveBy(orderId, levelId, approveBy);
	};

	/**
	 * 根据订单编号、当前等级、执行人名称和对象状态获取对象
	 */
	@Override
	public ApproveHistory getApproveHistoryByOrderIdAndLevelIdAndApproveByAndObjectState(String orderId, String levelId, String approveBy,String objectState){
		return this.approveHistoryMapper.selectByOrderIdAndLevelIdAndApproveByAndObjectState(orderId, levelId, approveBy,objectState);
	};


	/**
	 * 根据订单编号和当前等级获取全部审批状态
	 */
	@Override
	public List<String> getApproveStatusByOrderIdAndLevelIdAndObjectState1(String orderId, String levelId){
		return this.approveHistoryMapper.selectApproveStatusByOrderIdAndLevelIdAndObjectState1(orderId, levelId,"1");
	};

	/**
	 * 判断审批状态是否全部为”已通过“
	 */
	@Override
	public boolean isAllApproveStatus(String orderId, String levelId){
		List<String> approveStatusList = this.getApproveStatusByOrderIdAndLevelIdAndObjectState1(orderId, levelId);
		if(approveStatusList == null || approveStatusList.isEmpty()){
			return false;
		}
		for(String approveStatus : approveStatusList){
			if(!"已通过".equals(approveStatus)){
				return false;
			}
		}
		return true;
	};

	/**
	 * 根据orderId levelId删除
	 */
	@Override
	public Integer deleteApproveHistoryByOrderIdAndLevelId(String orderId, String levelId){
		return this.approveHistoryMapper.deleteByOrderIdAndLevelId(orderId, levelId);
	};

	/**
	 * 创建一个（评论为）ApproveHistory默认对象（含数据库操作）
	 * orderId levelId approveBy approveTime approveStatus comment creationdate objectstate
	 * orderId levelId approveBy 前端送递
	 * approveStatus = "未审批"
	 * comment = "默认"
	 * approveTime creationdate = 当前时间
	 * objectstate = "1"
	 */
	@Override
	public ApproveHistory createDefaultApproveHistory(String orderId, String levelId, String approveBy){
		ApproveHistory approveHistory = new ApproveHistory();

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		approveHistory.setOrderId(orderId);
		approveHistory.setLevelId(levelId);
		approveHistory.setApproveBy(approveBy);
		approveHistory.setApproveTime(sdf.format(day));
		approveHistory.setApproveStatus("未审批");
		approveHistory.setComment("默认");
		approveHistory.setObjectState("1");
		approveHistory.setCreationDate(sdf.format(day));
		this.add(approveHistory);

		return approveHistory;
	};

	/**
	 * 创建一个（评论为）ApproveHistory可编辑对象（含数据库操作）
	 * orderId levelId approveBy approveTime approveStatus comment creationdate objectstate
	 * orderId levelId approveBy comment 前端送递
	 * approveStatus = "未审批"
	 * approveTime creationdate = 当前时间
	 * objectstate = "1"
	 */
	public ApproveHistory createEditableApproveHistory(String orderId, String levelId, String approveBy,String comment){
		ApproveHistory approveHistory = new ApproveHistory();

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		approveHistory.setOrderId(orderId);
		approveHistory.setLevelId(levelId);
		approveHistory.setApproveBy(approveBy);
		approveHistory.setApproveTime(sdf.format(day));
		approveHistory.setApproveStatus("未审批");
		approveHistory.setComment(comment);
		approveHistory.setObjectState("1");
		approveHistory.setCreationDate(sdf.format(day));

		this.add(approveHistory);

		return approveHistory;
	};

	/**
	 * 更改（评论为）默认，单人审核状态
	 * orderId 订单号 前端递送
	 * approveTime = 当前时间
	 * approveStatus = 是否通过
	 */
	@Override
	public ApproveHistory updateDefaultApproveHistory(String orderId, String levelIdS,String approveBy,String approveStatus){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		ApproveHistory approveHistory = this.getApproveHistoryByOrderIdAndLevelIdAndApproveBy(orderId,levelIdS,approveBy);
		approveHistory.setApproveStatus(approveStatus);
		approveHistory.setApproveTime(sdf.format(day));
		this.updateApproveHistoryById(approveHistory,approveHistory.getId());

		return approveHistory;
	};

	/**
	 * 更改正在使用的审核状态为1的，（评论为）默认，单人审核状态
	 * orderId 订单号 前端递送
	 * approveTime = 当前时间
	 * approveStatus = 是否通过
	 */
	@Override
	public ApproveHistory updateDefaultApproveHistoryObjectState(String orderId, String levelIdS, String approveBy, String approveStatus, String objectState){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		ApproveHistory approveHistory = this.getApproveHistoryByOrderIdAndLevelIdAndApproveByAndObjectState(orderId,levelIdS,approveBy,objectState);
		approveHistory.setApproveStatus(approveStatus);
		approveHistory.setApproveTime(sdf.format(day));
		this.updateApproveHistoryById(approveHistory,approveHistory.getId());

		return approveHistory;

	};

	/**
	 * 更改（评论为）默认，单人审核状态
	 * orderId 订单号 前端递送
	 * approveTime = 当前时间
	 * approveStatus = 是否通过
	 */
	@Override
	public ApproveHistory updateCommentApproveHistory(String orderId, String levelIdS,String approveBy,String approveStatus,String comment){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		ApproveHistory approveHistory = this.getApproveHistoryByOrderIdAndLevelIdAndApproveBy(orderId,levelIdS,approveBy);
		approveHistory.setApproveStatus(approveStatus);
		approveHistory.setComment(comment);
		approveHistory.setApproveTime(sdf.format(day));
		this.updateApproveHistoryById(approveHistory,approveHistory.getId());

		return approveHistory;
	};

	/**
	 * 根据根据订单编号、当前等级id更新对象状态和编辑时间
	 * 	 * orderId 订单号 levelId 当前审核等级 输入
	 * 	 * approveTime = 当前时间
	 * 	 * objectStates = "0"
	 */
	@Override
	public Integer updateObjectStateAndApproveTimeByOrderIdAndLevelId(String orderId, String levelId){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		System.out.println(sdf.format(day));

		return this.approveHistoryMapper.updateApproveHistoriesObjectStateAndApproveTimeByOrderIdAndLevelId("0",sdf.format(day),orderId,levelId);
	};

	/**
	 * 根据审批人、对象状态查找所有符合条件的对象的订单编号
	 */
	@Override
	public List<String> findOrderIdsByApproveByAndObjectState (String approveBy, String objectState){
		return this.approveHistoryMapper.findOrderIdsByApproveByAndObjectState(approveBy,objectState);
	};

	/**
	 * 根据审批人、审批结果查找所有符合条件的对象的订单编号
	 */
	@Override
	public List<String> findOrderIdsByApproveByAndApproveStatus (String approveBy, String approveStatus){
		return this.approveHistoryMapper.findOrderIdsByApproveByAndApproveStatus(approveBy,approveStatus);
	};

	/**
	 * 根据根据订单编号、当前等级id更新对象状态和编辑时间
	 * 	 * orderId 订单号 levelId 当前审核等级 输入
	 * 	 * approveTime = 当前时间
	 * 	 * objectStates = "0"
	 */
	@Override
	public Integer updateObjectStateAndApproveTimeByOrderId(String orderId){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		System.out.println(sdf.format(day));

		return this.approveHistoryMapper.updateApproveHistoriesObjectStateAndApproveTimeByOrderId("0",sdf.format(day),orderId);
	};

	/**
	 * 根据根据订单编号查询所有复合的条目
	 * orderId 订单号 输入
	 * return List<ApproveHistory>
	 */
	@Override
	public List<ApproveHistory> findApproveHistoryByOrderId(String orderId){
		return this.approveHistoryMapper.findApproveHistoryByOrderId(orderId);
	};

}